from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2 import QtCore

class Signals(QtCore.QObject):
    info_signal = QtCore.Signal(str)
    key_signal = QtCore.Signal(str)
    mouse_signal = QtCore.Signal(dict)

Global_Signals = Signals()

class Window(QWidget):
    def __init__(self, parent):
        super().__init__(parent=parent)

    def keyPressEvent(self, event):
        if (str(event.key()) == '16777220'):
            Global_Signals.key_signal.emit('enter')
        if event.key() == Qt.Key_A:
            Global_Signals.key_signal.emit('a')
        if event.key() == Qt.Key_D:
            Global_Signals.key_signal.emit('d')

class Label(QLabel):
    x0 = 0
    y0 = 0
    x1 = 0
    y1 = 0
    flag = False
    printable = False
    #鼠标点击事件
    def mousePressEvent(self, event):
        self.flag = True
        if event.x() > self.pixmap().width():
            self.x0 = self.pixmap().width()
        elif event.x() < 0:
            self.x0 = 0
        else:
            self.x0 = event.x()
        if event.y() > self.pixmap().height():
            self.y0 = self.pixmap().height()
        elif event.y() < 0:
            self.y0 = 0
        else:
            self.y0 = event.y()
    #鼠标释放事件
    def mouseReleaseEvent(self, event):
        self.flag = False
        self.printable = True
    #鼠标移动事件
    def mouseMoveEvent(self, event):
        if self.flag:
            if event.x() > self.pixmap().width():
                self.x1 = self.pixmap().width()
            elif event.x() < 0:
                self.x1 = 0
            else:
                self.x1 = event.x()
            if event.y() > self.pixmap().height():
                self.y1 = self.pixmap().height()
            elif event.y() < 0:
                self.y1 = 0
            else:
                self.y1 = event.y()
            self.update()
    #绘制事件
    def paintEvent(self, event):
        super().paintEvent(event)
        '''
        print('now: [%d, %d, %d, %d]'%(self.x0, self.y0, self.x1, self.y1))
        print('self: [%d, %d, %d, %d];\nself.pixmap: [%d, %d]'%(self.x(), self.y(), self.width(), self.height(), self.pixmap().width(), self.pixmap().height()))
        '''
        rect = QRect(min(self.x0, self.x1), min(self.y1, self.y0), abs(self.x1-self.x0), abs(self.y1-self.y0))
        painter = QPainter(self)
        painter.setPen(QPen(Qt.red, 2, Qt.SolidLine))
        painter.drawRect(rect)

